<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="名称" prop="name"
                :rules="[
                  {required: true, message:'名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="设备唯一标识"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="位置" prop="place"
                :rules="[
                 ]">
              <el-input v-model="inputForm.place" placeholder="请填写位置"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="组名" prop="kioskGroup.id"
                :rules="[
                  {required: true, message:'组名不能为空', trigger:'blur'}
                 ]">
          <SelectTree
                      ref="kioskGroup"
                      :props="{
                          value: 'id',             // ID字段名
                          label: 'name',         // 显示名称
                          children: 'childNodes'    // 子级字段名
                        }"

                      url="/smis/kiosk/jfKioskGroup/treeData"
                      :value="inputForm.kioskGroup.id"
                      :clearable="true"
                      :accordion="true"
                      @getValue="kioskGroupChange"/>
           </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="楼层" prop="floor.id"
              :rules="[
                {required: true, message:'楼层不能为空', trigger:'blur'}
               ]">
            <SelectTree
              ref="floor"
              :props="{
                  value: 'id',             // ID字段名
                  label: 'showName',         // 显示名称
                  children: 'childNodes'    // 子级字段名
                }"
              url="/smis/floor/jfFloor/treeData"
              :value="inputForm.floor.id"
              :clearable="true"
              :accordion="true"
              @getValue="(value) => {inputForm.floor.id=value; $refs.selectPoint.showFloor(value);}"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="角度" prop="angle"
                :rules="[
                  {required: true, message:'角度不能为空', trigger:'blur'},
                  {validator: validator.isFloat, trigger:'blur'}
                 ]">
              <el-input-number v-model="inputForm.angle" controls-position="right" @change="angleChange" :min="0" :step="5" :max="359"></el-input-number>
              <!-- <el-input v-model="inputForm.angle" placeholder="请填写角度"     ></el-input> -->
           </el-form-item>
        </el-col>
       <!-- <el-col :span="12">
            <el-form-item label="x" prop="x"
                :rules="[
                  {required: true, message:'x不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.x" placeholder="请填写x"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="y" prop="y"
                :rules="[
                  {required: true, message:'y不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.y" placeholder="请填写y"     ></el-input>
           </el-form-item>
        </el-col> -->
        <el-col :span="24" style="height: 400px;">
          <el-form-item label="所在位置" prop="x"
              :rules="[
                {required: true, message:'请选择所在位置', trigger:'blur'}
               ]">
            <select-point ref="selectPoint" type='kiosk' @onMapClick="mapClick"></select-point>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import SelectPoint from '@/components/jfMap/selectPoint.vue'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          name: '',
          place: '',
          kioskGroup: {
            id: '',
            devType: '0'
          },
          floor: {
            id: ''
          },
          angle: '',
          x: '',
          y: '',
          z: ''
        }
      }
    },
    components: {
      SelectTree,
      SelectPoint
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建导购设备`
        } else if (method === 'edit') {
          this.title = '修改导购设备'
        } else if (method === 'view') {
          this.title = '查看导购设备'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/smis/kiosk/jfKiosk/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.jfKiosk)
              this.loading = false
              this.$refs.selectPoint.showFloor( this.inputForm.floor.id )
              this.$refs.selectPoint.changeMeshType(this.inputForm.kioskGroup.devType || '0')
              this.$refs.selectPoint.setDirMesh( this.inputForm.floor.id, this.inputForm.x, this.inputForm.y, this.inputForm.z, this.inputForm.angle)
            })
          } else {
            this.$refs.selectPoint.removeMarkPoint()
          }
          this.$refs.selectPoint.resize();
        })
      },
      kioskGroupChange(value, title, item) {
        this.inputForm.kioskGroup.id=value;
        if(item){
          this.inputForm.kioskGroup.devType = item.devType;
          this.$refs.selectPoint.changeMeshType(item.devType);
        }
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/smis/kiosk/jfKiosk/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      },
      mapClick (e) {
        this.inputForm.x = e.x
        this.inputForm.y = e.y
        this.inputForm.z = e.z
      },
      angleChange (e) {
        this.$refs.selectPoint.changeDir( e )
      }
    }
  }
</script>
